#include <Windows.h>
#include <stdint.h>
#include <conio.h>  // _kbhit
#include <stdio.h>
#include <signal.h> // SIGINT
#include <time.h>

#ifndef UINT16
#define UINT16 unsigned short
#endif

HANDLE hStdin = INVALID_HANDLE_VALUE;

// Input Buffering Windows
DWORD fdwMode, fdwOldMode;

// Check keypress interrupt in windows
UINT16 checkKey() {
	return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit(); 	
} 

void disableInputBuffer() {
	hStdin = GetStdHandle(STD_INPUT_HANDLE);
	GetConsoleMode(hStdin, &fdwOldMode);	// Save old mode
	fdwMode = fdwOldMode
			  ^ ENABLE_ECHO_INPUT	// No input echo
			  ^ ENABLE_LINE_INPUT;	// Return when one or more characters are available
	SetConsoleMode(hStdin, fdwMode);	// Set new mode
	FlushConsoleInputBuffer(hStdin);// Clear buffer
}

void restoreInputBuffering() {
	SetConsoleMode(hStdin, fdwOldMode);
}

// Handle interrupt
void handleInterrupt(int signal) {
	restoreInputBuffering();
	printf("\n");
	exit(-2);
}

int main(int argc,char** argv) {
	// Setup
	// signal(SIGINT, handleInterrupt);
	// disableInputBuffer();

	while (true) {
		if (_kbhit()) {
			_getche();
		}
	}

	// Shutdown
	// restoreInputBuffering();
	return 0;
}
